//
//  HNSH_SignHeader.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/4/22.
//

#import "HNSH_SignHeader.h"

@interface HNSH_SignHeaderItem : UIView

/* button */
@property (nonatomic, strong) UIButton *button;
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* douImgV */
@property (nonatomic, strong) UIImageView *douImgV;

/* signBgView */
@property (nonatomic, strong) UIView *signBgView;
/* signImgV */
@property (nonatomic, strong) UIImageView *signImgV;

@end

@implementation HNSH_SignHeaderItem

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
        self.layer.cornerRadius = 10.0f;
        self.layer.masksToBounds = YES;
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addAllViews];
    [self addAllLayout];
}

- (void)addAllViews {
    [self addSubview:self.button];
    [self addSubview:self.titleLabel];
    [self addSubview:self.douImgV];
    [self addSubview:self.signBgView];
    [self.signBgView addSubview:self.signImgV];
}

- (void)addAllLayout {
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(17);
    }];
    [self.douImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.mas_equalTo(34);
        make.width.height.mas_equalTo(27);
    }];
    [self.signBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    [self.signImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.signBgView);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(13);
    }];
}

#pragma mark lazy
- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _button;
}

- (UILabel *)titleLabel {
    if (!_titleLabel ) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIView *)signBgView {
    if (!_signBgView) {
        _signBgView = [UIView new];
        _signBgView.backgroundColor = [UIColor colorWithHexString:@"#F7E9C6"];
        _signBgView.hidden = YES;
    }
    return _signBgView;
}

- (UIImageView *)signImgV {
    if (!_signImgV) {
        _signImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_sign_qd_icon_wancheng"]];
    }
    return _signImgV;
}

- (UIImageView *)douImgV {
    if (!_douImgV) {
        _douImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_sign_qd_icon"]];
        _douImgV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _douImgV;
}

@end

@interface HNSH_SignHeader ()

/* bgView */
@property (nonatomic, strong) UIView *bgView;
/* button */
@property (nonatomic, strong) UIButton *button;
/* signAry */
@property (nonatomic, strong) NSArray *signAry;

/* qdLabel */
@property (nonatomic, strong) UILabel *qdLabel;

@end

@implementation HNSH_SignHeader

//MARK: Cycle Methods
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

- (void)setSignModel:(HNSH_SignModel *)signModel {
    _signModel = signModel;
    self.qdLabel.text = [NSString stringWithFormat:@"已连续签到 %@ 天",signModel.continue_days];
    self.qdLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:self.qdLabel.text SelectContent:signModel.continue_days CommonFont:self.qdLabel.font SpeFont:[UIFont boldSystemFontOfSize:24] Color:[UIColor appGreenColor]];
    
    if (!signModel) {
        return;
    }
    [self configButtons:self.button.isSelected ? self.signModel.sign_data : [self getCurrentListWithList:self.signModel.sign_data index:0]];
}

//获取包含当天的7天数据
- (NSArray *)getCurrentListWithList:(NSArray *)list index:(NSInteger)index {
    if (list.count - index < 7) {
        index = list.count - 7;
    }
    NSArray *ary = [list subarrayWithRange:NSMakeRange(index, 7)];
    NSLog(@"ary=====>%@",ary);
    for (HNSH_SignDataItem *item in ary) {
        if (item.today) {
            return ary;
        }
    }
    return [self getCurrentListWithList:list index:index+7];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self addSubview:self.bgView];
    
}

- (void)addAllLayout{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-20);
    }];
    
    [self configBottomView];
}

- (void)configButtons:(NSArray *)buttons {
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    [self.bgView layoutIfNeeded];
    CGFloat width = 38;
    CGFloat height = 68;
    CGFloat leftAndRightSpece = 13;
    CGFloat spece = (self.bgView.width-leftAndRightSpece*2-width*7)/6;
    for (int i = 0; i < buttons.count; i ++) {
        HNSH_SignDataItem *itemModel = buttons[i];
        HNSH_SignHeaderItem *item = [[HNSH_SignHeaderItem alloc]initWithFrame:CGRectMake(leftAndRightSpece + (spece+width)*(i%7), 53+(16+height)*(i/7), width, height)];
        item.titleLabel.text = itemModel.date;
        item.signBgView.hidden = !itemModel.status;
        
        [self.bgView addSubview:item];
//        [[item.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//            UIButton *select = x;
//            select.selected = !select.selected;
//            item.signBgView.hidden = !select.selected;
//        }];
    }
    
    
}

- (void)configBottomView {
    //签到
    UILabel *qdLabel = [[UILabel alloc]initWithFrame:CGRectMake(42, 15, 200, 33)];
    qdLabel.text = @"已连续签到 0 天";
    qdLabel.textColor = [UIColor appBlackLabelColor];
    qdLabel.font = [UIFont systemFontOfSize:16];
    self.qdLabel = qdLabel;
    [self addSubview:qdLabel];
    qdLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:qdLabel.text SelectContent:@"0" CommonFont:qdLabel.font SpeFont:[UIFont boldSystemFontOfSize:24] Color:qdLabel.textColor];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-50);
        make.left.mas_equalTo(36);
        make.right.mas_equalTo(-36);
        make.height.mas_equalTo(1);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button setImage:[UIImage imageNamed:@"b_circle_top_address_down"] forState:UIControlStateNormal];
    [self addSubview:button];
    self.button = button;
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(0);
        make.left.right.equalTo(line);
        make.bottom.equalTo(self.bgView.mas_bottom);
    }];
    __weak __typeof(&*self)weakSelf = self;
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        button.selected = !button.selected;
        if (weakSelf.headerBlock) {
            weakSelf.headerBlock(button.selected);
        }
    }];
}

#pragma mark lazy
-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 8;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}


@end
